﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using infoMngmntGUI.Control;
using System.Data;
using System.Data.Odbc;

namespace infoMngmntGUI.View
{
    /// <summary>
    /// Interaction logic for Admin.xaml
    /// </summary>
    public partial class Admin : IView
    {
        #region Construction

        /// <summary>
        /// Instantiates a new Admin Page object to display on the MainWindow.  The page
        /// is assigned a minimum access property of ADMIN (enum type) to only allow 
        /// an ADMIN user type display the page.  
        /// </summary>
        public Admin()
        {
            minimumAccess = Control.AccessManagement.UserType.ADMIN;
            InitializeComponent();
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Returns a string describing the contents being displayed on the page; in 
        /// this case, the page represents an Administration Interface
        /// </summary>
        /// <returns>Returns a description of the page, Administration Interface</returns>
        public override string ToString()
        {
            return "Administration Interface";
        }

        #endregion

        #region Private Helper Methods / Event Handlers

        /*
         * 
         */
        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            sqlTextBox.Text = "";
            dataGrid.ItemsSource = null;
        }

        /* 
         * 
         */
        private void executeButton_Click(object sender, RoutedEventArgs e)
        {
            dataGrid.HorizontalScrollBarVisibility = ScrollBarVisibility.Visible;
            dataGrid.VerticalScrollBarVisibility = ScrollBarVisibility.Visible;
            dataGrid.AutoGenerateColumns = true;

            string sqlStatement = sqlTextBox.Text.Trim();
            try
            {
                DataSet dataSet = new DataSet("Query Data Set");
                DataTable queryResults = DbConnection.Query(sqlStatement);
                dataSet.Tables.Add(queryResults);

                if (queryResults.Columns.Count == 0)
                {
                    MessageBox.Show("The SQL Query was successfully executed.", "SQL Executed", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                else
                {
                    dataGrid.ItemsSource = dataSet.Tables[0].DefaultView;
                }
            }
            catch (OdbcException)
            {
                MessageBox.Show("The SQL query could not be executed.\nPlease check your SQL syntax.", "Invalid SQL Query",
                    MessageBoxButton.OK, MessageBoxImage.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show("An exception occured executing the SQL query.\nException Message: " + ex.Message, "Invalid SQL Query",
                    MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
            

        /*
         * 
         */
        private void sqlTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (String.IsNullOrWhiteSpace(sqlTextBox.Text))
            {
                cancelButton.IsEnabled = false;
                executeButton.IsEnabled = false;
            }
            else
            {
                cancelButton.IsEnabled = true;
                executeButton.IsEnabled = true;
            }
        }

        #endregion
    }
}
